package com.example.demo.algorithm.lesson.basic.class2;

import com.example.demo.algorithm.lesson.basic.domain.DoubleNode;
import com.example.demo.algorithm.lesson.basic.domain.INode;
import com.example.demo.algorithm.lesson.basic.domain.Node;

import static com.example.demo.algorithm.lesson.basic.domain.LinkedListHelper.generateRandomDoubleLinkedList;
import static com.example.demo.algorithm.lesson.basic.domain.LinkedListHelper.printLinkedList;

/**
 * @author liweizhi
 * @date 2020/4/23
 */
public class ReverseList {
    public static Node reverseSinglyLinkedList(Node head) {
        Node pre = null;
        Node next;
        while (head != null) {
            next = head.getNext();
            head.setNext(pre);
            pre = head;
            head = next;
        }
        return pre;
    }

    public static DoubleNode reverseDoubleLinkedList(DoubleNode head) {
        DoubleNode pre = null;
        DoubleNode next;

        while (head != null) {
            next = head.getNext();

            head.setNext(pre);
            head.setLast(next);
            pre = head;

            head = next;
        }
        return pre;
    }


    public static void main(String[] args) {
        /*Node singlyLinkedList = generateRandomSinglyLinkedList(10, 100);
        printLinkedList(singlyLinkedList);
        Node reversedSinglyLinkedList = reverseSinglyLinkedList(singlyLinkedList);
        printLinkedList(reversedSinglyLinkedList);*/

        DoubleNode doubleLinkedList = generateRandomDoubleLinkedList(10, 100);
        printLinkedList(doubleLinkedList);
        DoubleNode reversedDoubleLinkedList = reverseDoubleLinkedList(doubleLinkedList);
        printLinkedList(reversedDoubleLinkedList);
    }
}
